package com.liwenfeng.diancanspringboot.controller.admin;

import com.liwenfeng.diancanspringboot.common.result.PageResult;
import com.liwenfeng.diancanspringboot.common.result.Result;
import com.liwenfeng.diancanspringboot.pojo.dto.*;
import com.liwenfeng.diancanspringboot.pojo.vo.OrderStatisticsVO;
import com.liwenfeng.diancanspringboot.pojo.vo.OrderVO;
import com.liwenfeng.diancanspringboot.service.OrderService;
import com.liwenfeng.diancanspringboot.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 订单管理
 */
@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "订单管理接口")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    /**
     * 订单搜索
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("订单搜索：{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 各个状态的订单数量统计
     */
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics() {
        OrderStatisticsVO orderStatisticsVO = orderService.statistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 按id获取订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> details(@PathVariable("id") Long id) {
        OrderVO orderVO = orderService.details(id);
        return Result.success(orderVO);
    }

    /**
     * 接单
     *
     * @return
     */
    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        orderService.confirm(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 拒单
     *
     * @return
     */
//    @PutMapping("/rejection")
//    @ApiOperation("拒单")
//    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
//        orderService.rejection(ordersRejectionDTO);
//        return Result.success();
//    }
//
    /**
     * 取消订单
     *
     * @return
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        orderService.cancel(ordersCancelDTO);
        return Result.success();
    }

//    /**
//     * 派送订单
//     */
//    @PutMapping("/delivery/{id}")
//    @ApiOperation("派送订单")
//    public Result delivery(@PathVariable("id") Long id) {
//        orderService.delivery(id);
//        return Result.success();
//    }
//
    /**
     * 完成订单（浏览器端）
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result complete(@PathVariable("id") Long id) {
        log.info("完成订单：{}", id);
        orderService.complete(id);
        return Result.success();
    }

    // 浏览器端修改orderDetail todo 用orderDetailController中的代替
//    @PutMapping("/dishUpdate")
//    @ApiOperation("修改订单")
//    public Result update(@RequestBody OrderUpdateDTO orderUpdateDTO) {
//        log.info("修改订单：{}", orderUpdateDTO);
//        orderService.updateDishUpdate(orderUpdateDTO);
//        return Result.success();
//    }

    @PostMapping("/autoAccept/{auto}")
    @ApiOperation("自动接单设置")
    public Result autoAccept(@PathVariable String auto) {
        // auto =2 为查询；=0或1 为设置：0为手动1为自动
        String autoStatus = auto;
        if (Objects.equals(auto, "2")){
            autoStatus = orderService.getAutoAccept();
        }else {
            orderService.setAutoAccept(auto);
        }
        return Result.success(autoStatus);
    }

    /**
     * 按桌号查询订单(浏览器端)
     */
    @GetMapping("/place/{place}")
    @ApiOperation("按桌号查询订单列表")
    public Result<List<OrderVO>> place(@PathVariable Integer place) {
        log.info("按桌号查询订单：{}", place);
        if (place == null) {
            return Result.error("桌号为空");
        }
        List<Long> turnoverIds = userService.getTurnoverIds(place);
        if (turnoverIds.size() == 0) {
            return Result.error("该桌未被使用");
        }
        List<OrderVO> orderVOList = orderService.getOrderListByTurnoverId(turnoverIds.get(0));
        return Result.success(orderVOList);
    }

    @PostMapping("/addOrder")
    @ApiOperation("添加订单")
    public Result addOrder(@RequestBody OrdersDTO ordersDTO) {
        log.info("添加订单：{}", ordersDTO);
        orderService.addOrder(ordersDTO);
        return Result.success();
    }

    @GetMapping("/list/{Status}")
    @ApiOperation("按状态查询订单")
    public Result<List<OrderVO>> list(@PathVariable Integer Status) {
        log.info("按状态查询订单：{}", Status);
        List<OrderVO> orderVOList = orderService.listByStatus(Status);
        return Result.success(orderVOList);
    }
}
